/*	$Id: thrpool.c 1118 2010-12-12 21:05:32Z phrakt $	*/
/*
 * Copyright (c) 2010 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/time.h>

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

#include <canopy/queue.h>
#include <canopy/string.h>

#include "log.h"
#include "mem.h"
#include "thread.h"
#include "private.h"





/*
 * cnp_thrpool_create()
 *
 * Create a new thread pool named <name>
 *
 * Returns 0 on success, or -1 on failure.
 */
cnp_thrpool_t*
cnp_thrpool_create(const char *name, int flags, sigset_t sigmask,
    void * (*thr_main)(void *), void *arg)
{
	int ret;
	cnp_thrpool_t *pool;

	if ((pool = cnp_malloc(sizeof(*pool))) == NULL) {
		cnp_log_err(cnp_logchan, "failed to allocate thread pool");
		return (NULL);
	}

	memset(pool, 0, sizeof(*pool));

	strlcpy(pool->tp_name, name, sizeof(pool->tp_name));

	pool->tp_flags = flags;
	pool->tp_main = thr_main;
	pool->tp_thrcount = 0;

	TAILQ_INIT(&pool->tp_threads);

	pthread_attr_init(&pool->tp_thrattr);

	if (pool->tp_flags & CNP_THRPOOL_DETACH)
		pthread_attr_setdetachstate(&pool->tp_thrattr, PTHREAD_CREATE_DETACHED);

	if ((ret = pthread_mutex_init(&pool->tp_lock, NULL)) != 0) {
		cnp_log_err(cnp_logchan,
		    "failed to initialize thread pool lock");
		return (NULL);
	}

	return (pool);
}

/*
 * cnp_thrpool_destroy()
 *
 * Shutdown all the threads in the thread pool and release pool resources.
 */
void
cnp_thrpool_destroy(cnp_thrpool_t *pool)
{
	int ret;
	cnp_thr_t *thr;

	if ((ret = pthread_mutex_lock(&pool->tp_lock)) != 0) {
		cnp_log_err(cnp_logchan, "failed to lock thread pool");
		return;
	}

	/* cancel all remaining active threads */
	TAILQ_FOREACH(thr, &pool->tp_threads, thr_link) {
		(void)cnp_thr_cancel(thr);

		if (!(pool->tp_flags & CNP_THRPOOL_DETACH))
			(void)cnp_thr_join(thr, NULL);
	}

	(void)pthread_attr_destroy(&pool->tp_thrattr);

	(void)pthread_mutex_unlock(&pool->tp_lock);
	(void)pthread_mutex_destroy(&pool->tp_lock);

	cnp_free(pool);
}

/*
 * cnp_thrpool_signal()
 *
 * Send the signal specified in <sig> to all the threads in the pool <pool>.
 *
 * Returns 0 on success, or -1 on failure.
 */
int
cnp_thrpool_signal(cnp_thrpool_t *pool, int sig)
{
	int ret;
	cnp_thr_t *thr;

	if ((ret = pthread_mutex_lock(&pool->tp_lock)) != 0) {
		cnp_log_err(cnp_logchan, "failed to lock thread pool");
	}

	TAILQ_FOREACH(thr, &pool->tp_threads, thr_link) {
		cnp_thr_signal(thr, sig);
	}

	(void)pthread_mutex_unlock(&pool->tp_lock);

	return (ret);
}
